package se.webbzon.oschi01.gui;

import java.io.IOException;
import java.util.Collection;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import se.webbzon.oschi01.action.RedoAction;
import se.webbzon.oschi01.action.UndoAction;
import se.webbzon.oschi01.command.History;
import se.webbzon.oschi01.singleton.Singletons;
import se.webbzon.oschi01.tools.Tool;
import se.webbzon.oschi01.tools.ToolLoader;
import se.webbzon.oschi01.tools.mapper.MapperToolAction;

public class MainMenuBar extends JMenuBar {
	
	public MainMenuBar(MainModel model) {
		JMenu file = new JMenu("File");
		file.add(new JMenuItem("Open File..."));
		add(file);
		
		JMenu edit = new JMenu("Edit");
		edit.add(new JMenuItem(new UndoAction(model.getHistory())));
		edit.add(new JMenuItem(new RedoAction(model.getHistory())));
		add(edit);
		
		JMenu tools = new JMenu("Tools");
		ToolLoader loader = Singletons.instance(ToolLoader.class);
		try {
			Collection<Tool> toolCollection = loader.loadTools();
			for (Tool tool : toolCollection) {
				tools.add(tool.createMenuItem());
			}
		} catch (IOException e) {
			e.printStackTrace();
			tools.add(new JMenuItem("Failed to load tools"));
		}
		add(tools);
	}

}
